import {getUrlParam} from '@/static/js/filter.js'
export default {
	data() {
		return {
			appConstitutionEntityVo:[],  //从缓存中获取
			submitAppConstitutionEntityVo:[], //点击提交
			calculatePoints: {},
			defaultArr:['平和质','气虚质','阳虚质','阴虚质','痰湿质','湿热质','血瘀质','气郁质','特禀质'],
			id:'',
			calculateStatus:false,
			currentIndex: 9,
			backId: '',
			mark: '',
			userStatus:''
		}
	},
	onLoad() {
		this.userStatus = getUrlParam('userStatus');
		if(!getUrlParam('between')){
			// this.$api.newScore({userId : getUrlParam('userId')}).then((res)=>{
			// 	if(res.data.returnCode == '200') {
			// 		this.calculatePoints = res.data.result && res.data.result.appConstitutionEntity;
			// 	}
			// })
			const id = getUrlParam('editId')
			this.$api.findByConstitutionId(id).then((res) => {
				if(res.data.returnCode == '200') {
					this.calculatePoints = res.data.result.appConstitutionEntity;
					if(this.calculatePoints.isAmended == 2) {
						this.calculateStatus = true;
					}
				}
			})
		} else if(getUrlParam('between')){
			this.appConstitutionEntityVo = localStorage.getItem('questionData') && JSON.parse(localStorage.getItem('questionData')).map((a) => {
				var bresult = a.data.map((b) => {
					var cresult = []
					cresult = b.options.filter((c) => {
						c.label = a.id
						return c.checked2
					})
					return cresult[0]
				})
				bresult = bresult.map((d)=>{
					return {
						label:d && d.label || '',
						userOption:d && d.userOption || '',
						userParame:d && d.userParame || '',
						orde: 0,
						healthOption: "string",
						healthParame: 0
					}
				})
				return bresult
			})
			var obj = []
			this.appConstitutionEntityVo && this.appConstitutionEntityVo.map((item,index) => {
				obj = obj.concat(item)
			});
			this.submitAppConstitutionEntityVo = {
				appConstitutionQuestionnaireEntities: obj,
				userId:Number(getUrlParam('userId')),
				type: 1,
				mark: 2,
				healthId: JSON.parse(localStorage.userInfo).id,
				status: 1,
			}
		}
	
	},
	methods: {
		 // 点击tab返回到上一页指定的位置
		back(e) {
			this.currentIndex = e;
			const editId = getUrlParam('editId') ? getUrlParam('editId') : ''; 
			const userId = getUrlParam('userId') ? getUrlParam('userId') : '';
			const status = getUrlParam('status') ? getUrlParam('status') : '';
			if(this.backId) {
				uni.navigateTo({
					url:'/pages/quickly/tcm/questionnaireDetailTCM2?current='+e+'&editId='+this.backId+'&userId='+userId+'&status='+this.status+'&mark='+this.mark+'&sex='+getUrlParam('sex')
				})
			} else if(editId && !this.userStatus) {
				uni.navigateTo({
					url:'/pages/quickly/tcm/questionnaireDetailTCM2?current='+e+'&editId='+editId+'&userId='+userId+'&status='+this.status+'&mark='+getUrlParam('mark')+'&sex='+getUrlParam('sex')
				})
			} else if(editId && this.userStatus) {
				uni.navigateTo({
					url:'/pages/quickly/tcm/questionnaireDetailTCM2?current='+e+'&editId='+editId+'&userStatus=1'+'&sex='+getUrlParam('sex')+'&userId='+userId
				})
			}else {
				uni.navigateTo({
					url:'/pages/quickly/tcm/questionnaireDetailTCM2?current='+e+'&userId='+userId+'&status='+status+'&mark='+getUrlParam('mark')+'&sex='+getUrlParam('sex')+'&between='+1
				})
			}
			
		},
		tips() {
			uni.showToast({
				title: '返回中医体质问卷查看详情吧~',
				icon: 'none'
			})
		},
		// 点击计算转化分
		calculate() {				
			if(getUrlParam('userId') && localStorage.getItem('questionData')) {
				this.submitAppConstitutionEntityVo.status = 2
				this.$api.memberIdList(this.submitAppConstitutionEntityVo).then((res) => {
					if(res.data.returnCode == '200') {
						localStorage.removeItem('questionData');
						this.calculatePoints = res.data.result && res.data.result;
						this.backId = res.data.result && res.data.result.id;
						this.mark = res.data.result && res.data.result.mark;
						localStorage.setItem('calculatePoints',JSON.stringify(this.calculatePoints))
					}
				})
			} else if(!localStorage.getItem('questionData')) {
				uni.showToast({
					title: '返回中医体质问卷查看详情吧~',
					icon: 'none'
				})
			}
		},
		// 返回列表
		page() {
			uni.navigateTo({
				url: '/pages/quickly/tcm/createTcm?sex='+getUrlParam('sex')
			})
			
		},
		
		// 望闻切详情
		details() {
			uni.navigateTo({
				url: '/pages/quickly/tcm/tcmInfo?id='+getUrlParam('editId')
			})
		},
		
		// 截取时间
		shijiantime(times){
			var timearr = times.replace(" ", ":").replace(/\:/g, "-").split("-");
			var timestr = ""+timearr[0]+"-" + timearr[1] + "-" + timearr[2]
			return timestr
		}
	}
}